package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.exp.GuiguException;
import com.atguigu.spzx.manager.mapper.ProductDetailsMapper;
import com.atguigu.spzx.manager.mapper.ProductMapper;
import com.atguigu.spzx.manager.mapper.ProductSkuMapper;
import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.entity.base.ProductUnit;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;
    @Autowired
    ProductSkuMapper productSkuMapper;
    @Autowired
    ProductDetailsMapper productDetailsMapper;


    @Override
    public List<ProductUnit> findAllProductUnit() {
        return productMapper.findAllProductUnit();
    }
    @Override
    public PageInfo findByPage(Integer pageNum, Integer pageSize, ProductDto productDto) {
        PageHelper.startPage(pageNum,pageSize);
        List<Product> list = productMapper.selectList(productDto);
        return new PageInfo(list);
    }

    @Override
    public void addProduct(Product product) {
        productMapper.addProduct(product);
        Long ProductId = product.getId();

        //添加sku
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (int i = 0; i < productSkuList.size(); i++) {
            ProductSku productSku = productSkuList.get(i);
            productSku.setSkuCode(ProductId+ "_"+i);
            productSku.setSkuName(product.getName()+ " "+productSku.getSkuSpec());
            productSku.setProductId(ProductId);

            productSkuMapper.addProdect(productSku);

        }

        String detailsImageUrls = product.getDetailsImageUrls();
        ProductDetails productDetails = new ProductDetails();
        productDetails.setImageUrls(detailsImageUrls);
        productDetails.setProductId(ProductId);
        productDetailsMapper.insertDetails(productDetails);
    }

    @Override
    public Product getProductById(Long productId) {
         //根据productid查product对象
        Product product = productMapper.getProductById(productId);
         // 查sku
        List<ProductSku> listSku = productSkuMapper.getByProductId(productId);
        product.setProductSkuList(listSku);
        //查details
       ProductDetails listDetails =   productDetailsMapper.getByProductId(productId);
       product.setDetailsImageUrls(listDetails.getImageUrls());

        return product;
    }

    @Override
    public void deletedById(Long id) {
        productMapper.deletedById(id);
    }

    @Override
    public void updateAuditStatus(Long productId, Integer auditStatus) {
        productMapper.updateAuditStatus(productId,auditStatus);
    }

    @Override
    public void updateStatus(Long productId, Integer status) {
        Product productById = productMapper.getProductById(productId);
        if (productById.getAuditStatus()!=1){
            throw new  GuiguException(400,"该spu商品审核未通过，暂不允许进行上下架操作");
        }


        if (status!=1 && status!=-1){
            throw new GuiguException(400,"该spu商品上下架的取值不正确");
        }


        productMapper.updateStatus( productId,status);
        productSkuMapper.updateStatus(productId,status);
    }

    @Override
    public void updateSkuId(Long skuId, Integer status) {

        ProductSku productSku = productSkuMapper.getById(skuId);
        Long productId = productSku.getProductId();
        Product product = productMapper.getProductById(productId);
        if (product.getAuditStatus()!=1){
            throw new  GuiguException(400,"当前sku所对应的spu商品审核未通过，暂不允许进行上下架操作");
        }

        if (product.getStatus()!=1){
            throw new GuiguException(400,"需先上架该sku所对应的spu");
        }
        productSkuMapper.updateSkuId(skuId,status);
    }

    @Override
    public void updateProduct(Product product) {
        productMapper.updateById(product);

        for (ProductSku productSku: product.getProductSkuList()){
            productSkuMapper.updateById(productSku);

        }

        String detailsImageUrls = product.getDetailsImageUrls();
        productDetailsMapper.updateById(product.getId(),detailsImageUrls);

    }
}
